<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: x4you
 * COPYRIGHT	: 2009
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 * 
 * </pre>
 * 
 * Created on	:  Mar 11, 2009
 * 
 * @author		hoadn <hoadn@rubik.com.vn>
 * @access		[public/private]
 * @package	[package name]
 *
 */
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls( "mvc.AbstractController" );

class SiteLanguageController extends AbstractController {

	/**
	 * Model of LanguageModel
	 *
	 * @var LanguageModel
	 */
	var $_model	=	NULL;
	
	/**
	 * Contrustor of EventController
	 *
	 */
	function __construct() {
		parent::__construct ();
		SysApplication::importModelCls ( "LanguageModel" );
		$this->_model = new LanguageModel ( );
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}

	function display() {
		$this->setTitle ( SysLanguage::_('MANAGEMENT') );
		//$this->addToolbarButton ( 'new' );
		$this->addToolbarButton ( 'edit' );
		//$this->addToolbarButton ( 'delete' );
		$msg = SysRequest::getVar ( "msg" );
		RClassesImporter::cls('dbo.dtos.SiteLanguageDTO');
		RCoreImporter::cls( "html.TableControl" );
		
		$table = new TableControl ( 'SiteLanguageDTO', NULL, $_REQUEST, 'siteLangTbl', '', 'ordering, id DESC' );
		$res = SysRequest::cloneResource();
		$table->setLinkRule('name', 'javascript:editTaskItem($key);');
		$table->setFieldHtmlInfo('flag',$this->_getPicFileFiledInfo('flag'));
		$table->removeField('id');
		echo "<p class=\"form-message\" style=\"text-align:left;\">
					{$msg}
				</p>";
		$table->printHtmlTable (SystemSite::getSiteConfig()->list_limit);
	}
	
	/**
	 * Function edit Event
	 *
	 * @param SiteLanguageDTO $langDTO
	 */
	
	function edit( $langDTO = null )
	{
		$this->setTitle ( SysLanguage::_('EDIT_TITLE') );
		$this->addToolbarButton ( "save" );
//		$this->addToolbarButton ( "apply" );
		$this->addToolbarButton ( "cancel" );
		$msg = SysRequest::getVar ( "msg" );
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $langDTO )) { 
			if (empty ( $cid )) {
					$this->setTitle ( SysLanguage::_('NEW_TITLE') );
			}
			if(is_array($cid)) {
				$cid = $cid[0];
			}				
			$langDTO = $oModel->loadSiteLang($cid);
		}
		
		RCoreImporter::cls( "html.FormFieldsControl" );			
		$formData = new FormFieldsControl ( $langDTO );
		$formData->setFieldHtmlInfo('flag',$this->_getPicFileFiledInfo('flag'));
		$htmlContent = $formData->generateHtml();
		eval('echo "'.SysApplication::loadTemplate('back/editEvent').'";');	
	}
	
	function save() {
		$oModel = $this->_model;		
		$langDTO = new SiteLanguageDTO();
		$langDTO->bind($_POST);
				
		$picField = $this->_getPicFileFiledInfo('flag'); 
		$langDTO->flag = $picField->getUploadedFilePath('files/site_language/');	
		
		$result = $oModel->saveSiteLanguage( $langDTO );										
		if ($langDTO->hasError())
			$msg = $langDTO->getAllErrorsMsg();			
//		die($this->getExecuteVal());
		if ($result === false || $this->getExecuteVal () === 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($langDTO);
		} else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));			
		}				
	}
	
	/**
	 * Function 
	 *
	 * @return boolean
	 */
	function delete() {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if($oModel->deleteSiteLang($cid)) {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));		
		}
		else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_DELETE_FAILED" ));
		}
	}
	
	 /** Enter description here...
	 *
	 * @param string $nextExec
	 * @return HtmlFieldInfo
	 */
	function &_getPicFileFiledInfo($inputName) {
		
		RCoreImporter::cls( "html.fieldInfo.FlashFileHtmlFieldInfo" );
		$field = new FlashFileHtmlFieldInfo($inputName,$linkRule);
		$field->setSupportedFilteType('.jpg,.jpeg,.bmp,.png,.gif');
		return $field;
	}
	
}
?>